﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Crank.Core
{
    class AutonamingModelTransformer : IModelTransformer
    {
        #region Blackboard Variables

        private static readonly IProperty<NounSpace> Nouns = Blackboard.Create<NounSpace>("Nouns");

        #endregion

        #region IModelTransformer Members

        public void ApplyTransform(DataModel target)
        {
            foreach (var entity in target.Entities.Values)
                foreach (var attribute in entity.Attributes)
                {
                    if (!attribute.Name.Contains("]"))
                        attribute.Name = attribute.Name.Replace("_", Nouns.Value.SingleOf(entity.Name));
                    else
                    {
                        Regex reg = new Regex(@"^\[(.*)\]$");
                        attribute.Name = reg.Replace( attribute.Name, "$1");
                    }
                }

        }

        #endregion
    }
}
